package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.R;
import com.meishij.entry.ShiHua_Datail_Entry;
import com.meishij.entry.ShihuaEntry;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 * 我是youngKaka 我的心愿是世界和平
 */

public class ShiHua_Detail_Adapter extends RecyclerView.Adapter<ShiHua_Detail_Adapter.ViewHolder> {

    private Context context;
    private List<Object> list;
    private int[] layoutid;
    private ShiHua_Detail_Comment_Adapter shiHua_detail_comment_adapter;


    public ShiHua_Detail_Adapter(Context context, List<Object> list, int[] layoutid) {
        this.context = context;
        this.list = list;
        this.layoutid = layoutid;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return layoutid[0];
        }else  if(position==1){
            return layoutid[1];
        }
        return -1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            throw new RuntimeException();
        }
        View view = LayoutInflater.from(context).inflate(viewType, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position == 0) {
            ShiHua_Datail_Entry.TopicBean topicBean = (ShiHua_Datail_Entry.TopicBean) list.get(0);
            holder.top_time.setText(topicBean.getTime() + "");
            holder.top_username.setText(topicBean.getUser_info().getUser_name() + "");
            Uri uri = Uri.parse(topicBean.getUser_info().getAvatar());
            holder.simpleDraweeView.setImageURI(uri);
            holder.top_container.removeAllViews();
            for (int i = 0; i < topicBean.getImgs().size(); i++) {
                ShiHua_Datail_Entry.TopicBean.ImgsBean imgsBean = topicBean.getImgs().get(i);
                View view = LayoutInflater.from(context).inflate(R.layout.item_shihua_detail_container_pic, null);
                SimpleDraweeView sdv = (SimpleDraweeView) view.findViewById(R.id.sdv_shihua_detail_top_pic);
                float ratio = Integer.parseInt(imgsBean.getWidth()) * 1.0f / Integer.parseInt(imgsBean.getHeight());
                sdv.setAspectRatio(ratio);
                sdv.setImageURI(Uri.parse(imgsBean.getBig()));
                holder.top_container.addView(view);
            }
            holder.zan_gridLayout.removeAllViews();
            holder.zan_gridLayout.setColumnCount(9);
            for (int i=0;i<topicBean.getZan_users().size();i++){
                ShiHua_Datail_Entry.TopicBean.ZanUsersBean zanUsersBean = topicBean.getZan_users().get(i);
                View view=LayoutInflater.from(context).inflate(R.layout.item_shihua_detail_user_avator,null);
                SimpleDraweeView simple = (SimpleDraweeView) view.findViewById(R.id.sdv_shihua_detail_top_pic);
                Uri parse=Uri.parse(zanUsersBean.getAvatar());
                simple.setImageURI(parse);
                holder.zan_gridLayout.addView(view);
            }
        } else if (position == 1) {
            List<ShiHua_Datail_Entry.CommentListBean> commentListBeen = (List<ShiHua_Datail_Entry.CommentListBean>) list.get(1);
            LinearLayoutManager layout = new LinearLayoutManager(context);
            layout.setOrientation(LinearLayoutManager.VERTICAL);
            holder.comment_container.setLayoutManager(layout);
            shiHua_detail_comment_adapter = new ShiHua_Detail_Comment_Adapter(context,
                    (List<ShiHua_Datail_Entry.CommentListBean>) list.get(1));
            holder.comment_container.setAdapter(shiHua_detail_comment_adapter);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        RecyclerView comment_container;
        GridLayout zan_gridLayout;
        SimpleDraweeView simpleDraweeView;
        TextView top_username;
        TextView top_time;
        LinearLayout top_container;

        public ViewHolder(View itemView) {
            super(itemView);
            simpleDraweeView = ((SimpleDraweeView) itemView.findViewById(R.id.sdv_shihua_detail_top_user_logo));
            top_username = ((TextView) itemView.findViewById(R.id.tv_shihua_detail_top_username));
            top_time = ((TextView) itemView.findViewById(R.id.tv_shihua_detail_top_time));
            top_container = ((LinearLayout) itemView.findViewById(R.id.linear_shihua_detail_top_container));
            zan_gridLayout = ((GridLayout) itemView.findViewById(R.id.grid_shihua_detail_zan_container));

            comment_container = ((RecyclerView) itemView.findViewById(R.id.recycler_shihua_detail_comment));
        }
    }

}
